local Class = require('lua.lang.Class')
local InputStream = require('lua.io.InputStream')

--- @param pathOrFile string | File
--- @return file
local function getLFile(pathOrFile)
	return io.open(type(pathOrFile) == 'string' and pathOrFile or
			pathOrFile.pathname, 'rb')
end

--- @class FileInputStream : InputStream
--- @field file file
local FileInputStream = Class {
	__extends = InputStream
}

--- @param pathOrFile string | File
--- @return FileInputStream
function FileInputStream:new(pathOrFile)
	--- @type FileInputStream
	local o = {}
	o.file = getLFile(pathOrFile)
	return setmetatable(o, FileInputStream)
end

--- @return number
function FileInputStream:read()
	local c = self.file:read(1)
	return c and c:byte() or -1
end

--- @return void
function FileInputStream:close()
	self.file:close()
	self.file = nil
end

return FileInputStream